package edu.gyc.hiblog.templates;

import com.github.pagehelper.PageInfo;
import edu.gyc.hiblog.service.PostService;
import edu.gyc.hiblog.util.RedisUtil;
import edu.gyc.hiblog.vo.PostVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import java.util.*;

//本周热议
@Component
@Slf4j
public class WeekRankTemplate extends TemplateDirective {
    @Autowired
    RedisUtil redisUtil;

    @Override
    public String getName() {
        return "hots";
    }

    @Override
    public void execute(DirectiveHandler handler) throws Exception {

        String weekRankKey = "week:rank";
        List<Map> hostPosts = new ArrayList<>();

        Set<ZSetOperations.TypedTuple> typedTuples = redisUtil.getZSetRank(weekRankKey, 0, 6);
        for (ZSetOperations.TypedTuple typedTuple : typedTuples) {
            Map<String, Object> map = new HashMap<>();
            Object value=typedTuple.getValue();//post的Id
            String postKey="rank:post:"+value;

            map.put("id", value);
            map.put("title", redisUtil.hget(postKey, "post:title"));
            map.put("commentCount", typedTuple.getScore());

            hostPosts.add(map);
        }

        handler.put(RESULTS,hostPosts).render();

    }
}
